SUBROUTINE WRTDCDF
USE PARKIND1  ,ONLY : JPIM     ,JPRB,JPRM,JPRD
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK, JPHOOK
! (C) Copyright 2000- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.

!**** *WRTDCDF*  - writes diagnostics to NetCDF files

!     Purpose.
!     --------
!        writes out diagnostic variables

!**   Interface.
!     ----------
!        *CALL* *WRTDCDF

!     Explicit arguments :
!     --------------------

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        None

!     Reference.
!     ----------

!     Author.
!     -------
!        Bart vd Hurk (KNMI)

!     Modifications.
!     --------------
!        original: 14/7/2000
!        E. Dutra - NEW OUTPUT FILE FOR LAKES : 04/07/2008
!        Y. Takaya - Add ocean mixed layer output : 07/10/2008
!        A. Agusti-Panareda - Atmospheric CO2 forcing: 17/11/2020
!        I. Ayan-Miguez - Added spatially distributed parameters: Oct 2023

USE YOMLUN1S , ONLY : NPOSEFL  ,NPOSWAT  ,NPOSSUS  ,NPOSSUB &
           &,NPOSEVA  ,NPOSCLD ,RMISS, NPOSLKE, NPOSOCD &
           &,NPOSCO2 ,NPOSVEG  ,NPOSEXT &
           &,NPOSBIO  ,NPOSTIL  ,NPOSVTY,NPOSGGD,NULOUT
USE YOMLOG1S , ONLY : LACCUMW  ,LRESET   ,NACCUR   ,NDIMCDF &
           &,LWREFL   ,LWRWAT   ,LWRSUB   ,LWRSUS   ,LWREVA &
           &,LWRCLD   ,LWRLKE   ,LWROCD &
           &,LWRCO2   ,LWRVEG  ,LWREXT   ,LWRBIO &
           &,LWRTIL   ,LWRVTY,LWRGGD,LNCSNC
USE YOMCT01S , ONLY : NSTART   ,NFRPOS
USE YOETHF   , ONLY : R2ES, R3LES, R3IES, R4LES, R4IES, R5LES, R5ALVCP, &
           &          R5IES, R5ALSCP, RALVDCP, RTICE, RTWAT, RTWAT_RTICE_R, &
           &          RTICECU, RTWAT_RTICECU_R, RALSDCP,RHOH2O

USE YOMGPD1S , ONLY : VFCVL    ,VFCVH    ,VFTVL    ,VFTVH    ,VFSOTY   ,&
           &VFOCDEPTH,VFZO     ,VDZ0F    ,VDZ0H    , VFCLAKEF, S3RWPWPM3D, S3RWSATM3D, &
           &S2RVCOVL2D, S2RVCOVH2D, S3RVROOTSAH3D, S3RVROOTSAL3D
USE YOMGC1S  , ONLY : LMASK
USE YOMDYN1S , ONLY : NSTEP    ,TSTEP
USE YOMRIP   , ONLY : RSTATI
USE YOEPHY   , ONLY : LEVGEN, LECTESSEL, LECMF1WAY, LEAIRCO2COUP

USE YOESOIL1S, ONLY : RDAW     ,RWPWP    ,RWSAT    
USE YOMDPHY  , ONLY : NLAT     ,NLON     ,NPOI ,NPOIOFF ,NPOIP, NTILES ,NVHILO, &
                    & NLALO    ,NCSS , NSOTY, NCOM, YDSURF !KPP
USE YOMGP1SA , ONLY : FSNNUA   ,TSLNUA   ,QLINUA   ,QLQNUA,&
            &TSNNUA   ,WRENUA   ,ASNNUA   ,RSNNUA,&
            &TLICENUA,TLMNWNUA,TLWMLNUA,TLBOTNUA,TLSFNUA,HLICENUA,HLMLNUA,&
            &WSNNUA
USE YOMGDI1S , ONLY :&
            &D1SRFLD2 ,D1SRFLU2 ,D1TRFLD2 ,D1TRFLU2 &
           &,D1STISRD2,D1STISRU2,D1STASF2 ,D1SWNJQ2 &
           &,D1SWLJQ2 ,D1SW1JBG2,D1SWAEXT2,D1SGHF2 &
           &,D1STITK2 ,D1SVEGTK2,D1STIFR2 ,D1SDSH2 &
           &,D1AHFS2  ,D1AHFL2  ,D1SAVTK2 ,D1SRADTK2 &
           &,D1SALB2  ,D1STNDH2 ,D1SWDS2  ,D1SW1C2 &
           &,D1SSDS2  ,D1SWLDS2 ,D1SNFR2  ,D1SNTFR2 &
           &,D1SNAFR2 &
           &,D1SLSRF2 ,D1SCRF2  ,D1SLSSF2 ,D1SCSF2  &
           &,D1STE2   ,D1STRO2  ,D1STSRO2 ,D1SMLT2  &
           &,D1SVTRA2 ,D1T2M2    ,D1D2M2            &
           &,D1STITRD2,D1STITRU2,D1STILE2,D1STIH2   &
           &,D1STIEVAP2,D1HFLUXRF2,D1WFLUXRF2,D1WSN2 &
! CO2	   
           &,D1SAN2,D1SAG2,D1SRD2,D1SRSOIL_STR2,D1SRECO2,D1SCO2FLUX2,D1SCH4FLUX2 &
           &,D1SVTAN2,D1SVTAG2,D1SVTRD2,D1SVTRSOIL_STR2,D1SVTRECO2,D1SVTCO2FLUX2,D1SVTRNOQ102 &
           &,D1SBVOCFLUX2 &
! LAI, biomass
           &,D1SLAI2,D1SBIOM2,D1SBLOSS2,D1SBGAIN2,D1SBIOMSTR2,D1SBIOMSTRB2 &
           &,D1SVTLAI2,D1SVTBIOM2,D1SVTBLOSS2,D1SVTBGAIN2 &
           &,D1SVTBIOMSTR2,D1SVTBIOMSTRB2 &
! vegetation variables
           &,D1SVTGC2 ,D1SVTGA2 ,D1SVTF22 &
           &,D1SVTLE2 ,D1SVTFR2 ,D1SVTDS2 ,D1SVTDMAX2 &
! fractions
           &,D1SWFR2 &
! tiled ouput
           &,D1STIH2,D1STILE2,D1STIALB2 &
! extra WATER
           &,D1SWLIT2,D1SWAGFL2,D1SWDSSL2 &
! extra FORCING (Alterra validation)	
           &,D1STAIR2, D1SQAIR2, D1SUWIND2, D1SVWIND2,D1STIEVAPU2 &
           &,D1STNGFL2,D1STNM2,D1STISKC2,D1SPSURF2, DCMFCOM &
! extra FORCING (atmospheric CO2)
           &,D1SCO2AIR2 &
! BVOC emissions
           &,D1SBVOCST1,D1SBVOCST2

USE YOMCST   , ONLY : RTT      ,RDAY,  RMV   ,  RMD, RLVTT, RLSTT
USE YOMGP1S0 , ONLY :TRENU0
USE MPL_MODULE

IMPLICIT NONE

!* Netcdf Interface
INTEGER NVARID,NPOS,NSTPP,IERR
INTEGER ISTART1(1),ISTART3(3),ISTART4(4)
INTEGER ICOUNT1(1),ICOUNT3(3),ICOUNT4(4),ICOUNT4O(4)
REAL(KIND=JPRM),ALLOCATABLE :: ZOUTPUTS(:,:)
REAL(KIND=JPRD),ALLOCATABLE :: ZOUTPUT(:,:)
REAL(KIND=JPRD) :: ZTIME

!* Local variables
INTEGER(KIND=JPIM), PARAMETER :: JPNCDF=14  !KPP 7+6CTESSEL
REAL(KIND=JPRB),ALLOCATABLE :: ZVALUE(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZSNOFR(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZICEFR(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZQLINUA(:,:) 
REAL(KIND=JPRB),ALLOCATABLE :: ZLIQ(:,:)
REAL(KIND=JPRB),ALLOCATABLE :: ZSWET(:)   
REAL(KIND=JPRB),ALLOCATABLE :: ZSROOT(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZFDEPT(:)  
REAL(KIND=JPRB),ALLOCATABLE :: ZFTHAW(:)    
REAL(KIND=JPRB),ALLOCATABLE :: ZSNDEP(:)  
REAL(KIND=JPRB),ALLOCATABLE :: ZACOND(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZBUF(:)
INTEGER(KIND=JPIM) IPOS(JPNCDF)
LOGICAL LPOS(JPNCDF)
INTEGER(KIND=JPIM) :: IX1,IX2,IY1,IY2,NLEN,IST,IEN,IA,IYYMD,IHM,&
     &  ITYPL,ITYPH,JL,JK,JS,ISLOT,J,JTILE,JVT,ISTP,IENP
REAL(KIND=JPRB) :: ZWA,ZMM,ZMM2,ZPA,ZWPWP,ZWSAT,ZCVL,ZCVH,ZBOT,ZF,&
     &  ZTOP,RLMLT,ZIHCAP,ZEPSILON
REAL(KIND=JPRD) :: ZJUL
REAL(KIND=JPRB), ALLOCATABLE :: ZSSN(:),ZRSN(:),ZTSN(:),ZWSN(:),ZTMP1(:)
INTEGER(KIND=JPIM) :: MYPROC, NPROC
INTEGER(KIND=JPIM) :: NMXNV
REAL(KIND=JPHOOK) :: ZHOOK_HANDLE

#include "surf_inq.h"
#include "netcdf.inc"
#include "fcttre.h"

IF (LHOOK) CALL DR_HOOK('WRTDCDF',0,ZHOOK_HANDLE)

MYPROC = MPL_MYRANK()
NPROC  = MPL_NPROC()

ISTP   = NPOIOFF(1)+1
IENP   = NPOIOFF(NPROC+1)
WRITE(NULOUT,*) 'WRTDCDF'


ZEPSILON=10._JPRB*EPSILON(ZEPSILON)

IF (LEVGEN) THEN
  CALL SURF_INQ(YDSURF,PRDAW=RDAW)
ELSE
  CALL SURF_INQ(YDSURF,PRDAW=RDAW,PRWSAT=RWSAT,PRWPWP=RWPWP)
ENDIF

IF(NDIMCDF == 2)THEN
  IX1=1
  IX2=NLON
  IY1=1
  IY2=NLAT
  NLEN=(IX2-IX1+1)*(IY2-IY1+1)
  IST=IX1+(IY1-1)*NLON
  IEN=IST+NLEN-1
ELSE
  IST=1
  IEN=NLALO
  NLEN=IEN-IST+1
ENDIF

NMXNV=MAX(NCSS,NCOM+1)

IF(LWRVEG .OR. LWRVTY) NMXNV=MAX(NMXNV,NVHILO)



IF(LWRTIL) NMXNV=MAX(NMXNV,NTILES)

ALLOCATE (ZOUTPUT(NLEN,NMXNV))        !INFLATED OUTPUT ARRAY
IF( NACCUR==1 ) THEN
  ALLOCATE (ZOUTPUTS(NLEN,NMXNV))       !INFLATED OUTPUT ARRAY
ENDIF

!ALLOCATE (ZOUTPUT3(NLEN,NCSS))  !INFLATED OUTPUT ARRAY
!ALLOCATE (ZOUTPUT3S(NLEN,NCSS)) !INFLATED OUTPUT ARRAY
!ALLOCATE (ZOUTOCN3(NLEN,NCOM+1))   !KPP
!ALLOCATE (ZOUTOCN3S(NLEN,NCOM+1))  !KPP

ALLOCATE (ZBUF(NLEN))
IF (LACCUMW) THEN
  IF(LRESET)THEN
!     ZWA - FACTOR TO TRANSFORM W/M**2*S INTO W/M**2
!     ZMM - FACTOR TO TRANSFORM KG/M**2 INTO MM/S (AVERAGING)
!     ZMM2 - FACTOR TO TRANSFORM KG/M**2 PER MODEL TIMESTEP INTO MM
!            PER OUTPUT TIME STEP (ACCUMULATING)
    ZWA=1._JPRB/(NFRPOS*TSTEP)
    ZMM=1._JPRB/(NFRPOS*TSTEP)
    ZMM2=1._JPRB/TSTEP
  ELSE
!     ZWA - FACTOR TO TRANSFORM W/M**2*S INTO W/M**2
!     ZMM - FACTOR TO TRANSFORM KG/M**2 INTO MM
    IF(NSTEP == 0)THEN
      ZWA=0._JPRB
    ELSE
      ZWA=1._JPRB/(NSTEP*TSTEP)
    ENDIF
    ZMM=1._JPRB
    ZMM2=1._JPRB
  ENDIF
  IA=2
ELSE
!     ZWA - FACTOR TO TRANSFORM W/M**2 INTO W/M**2
!     ZMM - FACTOR TO TRANSFORM KG/M**2/S INTO MM/DAY
  ZWA=1._JPRB
  ZMM=RDAY
  ZMM2=1._JPRB
  IA=1
ENDIF

IF(NSTEP == NSTART)THEN
  ZPA=1._JPRB
ELSE
  ZPA=1._JPRB/NFRPOS
ENDIF

!*     UNIT CONVERSION OF PROGNOSTIC QUANTITIES

ALLOCATE (ZQLINUA(NPOI,NCSS)) !RESCALED SOIL WATER
ALLOCATE (ZLIQ(NPOI,NCSS))    !LIQUID SOIL WATER
ALLOCATE (ZSWET(NPOI))        !TOTAL SOIL WETNESS
ALLOCATE (ZSROOT(NPOI))       !ROOT AVAILABLE SOIL WATER
ALLOCATE (ZFDEPT(NPOI))       !DEPTH OF FROZEN SOIL
ALLOCATE (ZFTHAW(NPOI))       !DEPTH OF THAW LAYER
ALLOCATE (ZSNDEP(NPOI))       !SNOW DEPTH
ALLOCATE (ZSNOFR(NPOI))       !SNOW FRACTION
ALLOCATE (ZICEFR(NPOI))       !ICE FRACTION
ALLOCATE (ZACOND(NPOI))       !AERODYNAMIC RESISTANCE

!* calculating average aerodynamic conductance
!DO JL=1,NPOI
!   ZACOND(JL)=1.0_JPRB/D1SVTRA2(JL,1,IA)
!ENDDO

DO JK=1,NCSS
  ZQLINUA(1:NPOI,JK)=QLINUA(1:NPOI,JK)*RDAW(JK)*RHOH2O
  ZLIQ(1:NPOI,JK)=QLQNUA(1:NPOI,JK)
ENDDO


!* store instantaneous (index=1) snow and ice fraction for later use
ZSNOFR(1:NPOI)=D1SNFR2(1:NPOI,1)
ZICEFR(1:NPOI)=D1STIFR2(1:NPOI,2,1)

!* calculation of snow depth
ZSNDEP(:)=SUM(FSNNUA(:,:)/RSNNUA(:,:),DIM=2)

! RTT=273.16_JPRB
RLMLT=2.8345E+6_JPRB-2.5008E+6_JPRB
ZIHCAP=2.05E6_JPRB/920._JPRB
ALLOCATE (ZSSN(NPOI))
ALLOCATE (ZRSN(NPOI))
ALLOCATE (ZTSN(NPOI))
ALLOCATE (ZWSN(NPOI))
ALLOCATE (ZTMP1(NPOI))
! Total snow variables
ZSSN(:) = SUM(FSNNUA(:,:)*ZPA,DIM=2)
ZTMP1(:) = SUM(ZIHCAP*FSNNUA(:,:)*ZPA*(TSNNUA(:,:)*ZPA-RTT) - &    ! INTERNAL ENERGY
              RLMLT*(FSNNUA(:,:)*ZPA-WSNNUA(:,:)*ZPA),DIM=2)
WHERE (ZSSN > ZEPSILON .AND. ZSNDEP > ZEPSILON )
  ZRSN = ZSSN / ZSNDEP
  ZTSN = MIN(RTT, RTT + (ZTMP1+RLMLT*ZSSN)/(ZIHCAP*ZSSN))
  ZWSN = MAX(0._JPRB,ZSSN+ZTMP1/RLMLT)
ELSEWHERE
  ZRSN = 100._JPRB
  ZTSN = RTT
  ZWSN = 0._JPRB
ENDWHERE

!* calculation of total soil wetness

!DO JL=1,NPOI
!  IF(LEVGEN)THEN
!    JS=NINT(VFSOTY(JL))
!    ZWPWP=RWPWPM(JS)
!    ZWSAT=RWSATM(JS)
!  ELSE
!    ZWPWP=RWPWP
!    ZWSAT=RWSAT
!  ENDIF
!  IF ( (ZWSAT-ZWPWP) > 0._JPRB ) THEN
!    ZSWET(JL)=&
!     &(DOT_PRODUCT(ZPA*QLINUA(JL,:),RDAW(:))-&
!     &SUM(RDAW)*ZWPWP)/&
!     &(SUM(RDAW)*(ZWSAT-ZWPWP))
!  ELSE
!    ZSWET(JL)=0.0_JPRB
!  ENDIF
!ENDDO

!* calculation of root zone soil moisture content: all liquid water
!  above wilting point in layers where roots are found

DO JL=1,NPOI

  ZCVL=VFCVL(JL)*S2RVCOVL2D(JL)
  ZCVH=VFCVH(JL)*S2RVCOVH2D(JL)

  ZSROOT(JL)=0._JPRB
  DO JK=1,NCSS
    IF(LEVGEN)THEN
      ZWPWP=S3RWPWPM3D(JL,JK)
    ELSE
      ZWPWP=RWPWP
    ENDIF
    IF((ZCVL > 0._JPRB .AND. S3RVROOTSAL3D(JL,JK) > 0._JPRB) .OR.&
      &(ZCVH > 0._JPRB .AND. S3RVROOTSAH3D(JL,JK) > 0._JPRB)) THEN
       ZSROOT(JL)=ZSROOT(JL)+&
        &MAX(ZPA*ZLIQ(JL,JK)-RDAW(JK)*RHOH2O*ZWPWP, 0.0_JPRB)
    ENDIF
  ENDDO
ENDDO

!* calculation of depth of frozen ground and thaw layer
DO JL=1,NPOI
  ZFDEPT(JL)=0._JPRB
  ZBOT=0._JPRB
  DO JK=1,NCSS
    ZF = 0_JPRB
    IF (ZQLINUA(JL,JK) > 0._JPRB ) THEN
      ZF=MAX(0._JPRB,MIN(1._JPRB,1._JPRB-QLQNUA(JL,JK)/ZQLINUA(JL,JK)))
    ENDIF
    ZTOP=ZBOT
    ZBOT=ZTOP+RDAW(JK)
    IF(ZF > 0._JPRB)THEN
      ZFDEPT(JL)=MIN(SUM(RDAW),ZTOP+ZF*(ZBOT-ZTOP))
    ENDIF
  ENDDO

  ZBOT=SUM(RDAW)
  ZFTHAW(JL)=ZBOT
  DO JK=NCSS,1,-1
    ZF=0._JPRB
    IF ( ZQLINUA(JL,JK) > 0._JPRB ) THEN
      ZF=MAX(0._JPRB,MIN(1._JPRB,1._JPRB-QLQNUA(JL,JK)/ZQLINUA(JL,JK)))
    ENDIF
    ZTOP=ZBOT-RDAW(JK)
    IF(ZF > 0._JPRB)THEN
      ZFTHAW(JL)=MAX(0._JPRB,ZBOT+ZF*(ZTOP-ZBOT))
    ENDIF
    ZBOT=ZTOP
  ENDDO
ENDDO

!* ===============================================================
!     START OUTPUT
!* ===============================================================

IPOS(1)=NPOSEFL
IPOS(2)=NPOSWAT
IPOS(3)=NPOSSUS
IPOS(4)=NPOSSUB
IPOS(5)=NPOSEVA
IPOS(6)=NPOSCLD
IPOS(7)=NPOSOCD
IPOS(8)=NPOSCO2
IPOS(9)=NPOSBIO
IPOS(10)=NPOSVEG
IPOS(11)=NPOSEXT
IPOS(12)=NPOSTIL
IPOS(13)=NPOSVTY
IPOS(14)=NPOSGGD

      
LPOS(1)=LWREFL
LPOS(2)=LWRWAT
LPOS(3)=LWRSUS
LPOS(4)=LWRSUB
LPOS(5)=LWREVA
LPOS(6)=LWRCLD
LPOS(7)=LWROCD
LPOS(8)=LWRCO2
LPOS(9)=LWRBIO
LPOS(10)=LWRVEG
LPOS(11)=LWREXT
LPOS(12)=LWRTIL
LPOS(13)=LWRVTY
LPOS(14)=LWRGGD

!     time level
!CALL DATTIM(zjul,IYYMD,IHM)

! when accumulated or averaged output is required, output is backwar
! averaged, and output just before the first time step is skipped

IF(LACCUMW)THEN
  ISTART1(1)=(NSTEP-NSTART)/NFRPOS
ELSE
  ISTART1(1)=(NSTEP-NSTART)/NFRPOS+1
ENDIF
ISLOT=ISTART1(1)
NSTPP=NSTEP
ZTIME=REAL(RSTATI,KIND=JPRD)-REAL(0.5_JPRB*TSTEP,KIND=JPRD)
IF (ISLOT < 1) RETURN
IF( MYPROC == 1 ) THEN
  DO J=1,JPNCDF
    IF(LPOS(J))THEN
      NPOS=IPOS(J)
      NVARID = NCVID(NPOS,'time',IERR)
      CALL NCVPT1 (NPOS,NVARID,ISTART1,ZTIME,IERR)
      NVARID = NCVID(NPOS,'timestp',IERR)
      CALL NCVPT1 (NPOS,NVARID,ISTART1,NSTPP,IERR)
    ENDIF
  ENDDO
ENDIF

IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=ISLOT
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=1

  ISTART4(1)=IX1
  ISTART4(2)=IY1
  ISTART4(3)=1
  ISTART4(4)=ISLOT
  ICOUNT4(1)=IX2-IX1+1
  ICOUNT4(2)=IY2-IY1+1
  ICOUNT4(3)=NCSS
  ICOUNT4(4)=1
ELSE
  ISTART3(1)=IST
  ISTART3(2)=ISLOT
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=1

  ISTART4(1)=IST
  ISTART4(2)=1
  ISTART4(3)=ISLOT
  ICOUNT4(1)=NLEN
  ICOUNT4(2)=NCSS
  ICOUNT4(3)=1
ENDIF

ALLOCATE (ZVALUE(NPOI))

IF(LWREFL)THEN
!* -- The surface fluxes
!******************************************************************
  NPOS = NPOSEFL

!     net shortwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SWnet',IERR)
  ZVALUE(:)=(D1SRFLD2(:,IA)+D1SRFLU2(:,IA))*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     net longwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LWnet',IERR)
  ZVALUE(:)=(D1TRFLD2(:,IA)+D1TRFLU2(:,IA))*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     latent heat flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qle',IERR)
  ZVALUE(:)=D1AHFL2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     sensible heat flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qh',IERR)
  ZVALUE(:)=D1AHFS2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     soil heat flux (conduction of Tsk - T1 + shortwave transmission)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qg',IERR)
  ZVALUE(:)=D1SGHF2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
 
!     snow basal heat flux 
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qgsn',IERR)
  ZVALUE(:)=D1STNGFL2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
!     snow phase changes
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qfsn',IERR)
  ZVALUE(:)=D1STNM2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     soil freezing flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qf',IERR)
  ZVALUE(:)=SUM(D1STASF2(:,:,IA),DIM=2)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     soil heat content change
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'DelSoilHeat',IERR)
  ZVALUE(:)=D1SDSH2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     snow heat content change
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'DelColdCont',IERR)
  ZVALUE(:)=D1STNDH2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     upward longwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LWup',IERR)
  ZVALUE(:)=D1TRFLU2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!    SWdown
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SWdown',IERR)
  ZVALUE(:)=D1SRFLD2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !    SWdown
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LWdown',IERR)
  ZVALUE(:)=D1TRFLD2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF


ENDIF

IF(LWRWAT)THEN
!* -- Water balance
!******************************************************************
  NPOS = NPOSWAT

!     snowfall
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Snowf',IERR)
  ZVALUE(:)=(D1SLSSF2(:,IA)+D1SCSF2(:,IA))*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     rainfall
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Rainf',IERR)
  ZVALUE(:)=(D1SLSRF2(:,IA)+D1SCRF2(:,IA))*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     evapotranspiration
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Evap',IERR)
  ZVALUE(:)=D1STE2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     surface runoff
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qs',IERR)
  ZVALUE(:)=-(D1STSRO2(:,IA))*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     Subsurface runoff
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qsb',IERR)
  ZVALUE(:)=-(D1STRO2(:,IA)-D1STSRO2(:,IA))*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     snowmelt
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qsm',IERR)
  ZVALUE(:)=D1SMLT2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     soil water storage change
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'DelSoilMoist',IERR)
  ZVALUE(:)=D1SWDS2(:,IA)*ZMM2
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     snow water storage change
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'DelSWE',IERR)
  ZVALUE(:)=D1SSDS2(:,IA)*ZMM2
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     interception water storage change
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'DelIntercept',IERR)
  ZVALUE(:)=D1SWLDS2(:,IA)*ZMM2
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN  
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     interception 
  IF( MYPROC == 1 )NVARID = NCVID(NPOS,'Intercept',IERR)
  ZVALUE(:)=D1SWLIT2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  IF (LECMF1WAY) THEN
  !     Flood Fraction
    IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'fldfrc',IERR)
    ZVALUE(:)=VFCLAKEF
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) THEN 
      ZOUTPUT(:,1)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
      IF(NACCUR == 1)THEN
        ZOUTPUTS(:,1)=ZOUTPUT(:,1)
        CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
      ELSE
        CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
      ENDIF
    ENDIF
  ENDIF
! 
!   !     soil water storage change per soil layer 
!   NVARID = NCVID(NPOS,'DelSoilMoistLayer',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=D1SWDSSL2(:,JK,IA)*ZMM2
!     ZOUTPUT3(:,JK)= UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF
! 
!  !     root extraction per soil layer
!   NVARID = NCVID(NPOS,'RootExtLayer',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=D1SWAEXT2(:,JK,IA)*ZMM
!     ZOUTPUT3(:,JK)= UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF
! 
!  !     Ground water flux per soil layer
!   NVARID = NCVID(NPOS,'GWLayer',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=D1SWAGFL2(:,JK,IA)*ZMM
!     ZOUTPUT3(:,JK)= UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF


ENDIF
      
IF(LWRSUS)THEN
!* -- Surface state variables
!******************************************************************
  NPOS = NPOSSUS

!     momentum roughness
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'z0m',IERR)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDZ0F(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:VDZ0F")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     heat roughness
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'lz0h',IERR)
  ZVALUE(:)=LOG(VDZ0H(:))
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:VDZ0H")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     vegetation temperature: area weighted average over tiles 4 & 6
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'VegT',IERR)
  ZVALUE(:)=D1SVEGTK2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     bare soil temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'BaresoilT',IERR)
  ZVALUE(:)=D1STITK2(:,8,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF


!     surface radiative temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'RadT',IERR)
  ZVALUE(:)=D1SRADTK2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     surface albedo
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Albedo',IERR)
  ZVALUE(:)=D1SALB2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* -- LAI
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'lai',IERR)
  ZVALUE(:)=D1SLAI2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF  
  ENDIF


ENDIF

IF (LWRGGD) THEN
  NPOS = NPOSGGD

  !     soil moisture
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SoilMoist',IERR)
  DO JK=1,NCSS
    ZVALUE(:)=ZQLINUA(:,JK)*ZPA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ENDIF
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NCSS)=ZOUTPUT(:,1:NCSS)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NCSS),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NCSS),IERR)
    ENDIF
  ENDIF

  !     soil temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SoilTemp',IERR)
  DO JK=1,NCSS
    ZVALUE(:)=TSLNUA(:,JK)*ZPA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ENDIF
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NCSS)=ZOUTPUT(:,1:NCSS)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NCSS),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NCSS),IERR)
    ENDIF
  ENDIF

  !     average surface temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'AvgSurfT',IERR)
  ZVALUE(:)=D1SAVTK2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     Canopy interception
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'CanopInt',IERR)
  ZVALUE(:)=WRENUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     snow water equivalent
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SWE',IERR)
  ZVALUE(:)=ZSSN
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     snow temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SnowT',IERR)
  ZVALUE(:)=ZTSN
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* -- Snow albedo
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SAlbedo',IERR)
  ZVALUE(:)=ASNNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
  !* -- Snow albedo
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'snowdens',IERR)
  ZVALUE(:)=ZRSN
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
  !* -- Snow LIQUID WATER
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'slw',IERR)
  ZVALUE(:)=ZWSN*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* -- Lake ice temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TLICE',IERR)
  ZVALUE(:)=TLICENUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* --lake mean water temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TLMNW',IERR)
  ZVALUE(:)=TLMNWNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* --lake mix layer water temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TLWML',IERR)
  ZVALUE(:)=TLWMLNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* --lake bottom water temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TLBOT',IERR)
  ZVALUE(:)=TLBOTNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
 
  !* --lake mix layer shape factor
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TLSF',IERR)
  ZVALUE(:)=TLSFNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
 
  !* --lake ice thickness
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'HLICE',IERR)
  ZVALUE(:)=HLICENUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !* --lake mixed layer depth
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'HLML',IERR)
  ZVALUE(:)=HLMLNUA(:)*ZPA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

ENDIF ! GGD

IF(LWRSUB)THEN

!* -- Sub-surface state variables
!******************************************************************
  NPOS = NPOSSUB

! !     soil temperature
!   NVARID = NCVID(NPOS,'SoilTemp',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=TSLNUA(:,JK)*ZPA
!     ZOUTPUT3(:,JK)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF
! !     soil moisture
!   NVARID = NCVID(NPOS,'SoilMoist',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=ZQLINUA(:,JK)*ZPA
!     ZOUTPUT3(:,JK)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF

! !     liquid soil moisture
!   NVARID = NCVID(NPOS,'LSoilMoist',IERR)
!   DO JK=1,NCSS
!     ZVALUE(:)=ZLIQ(:,JK)*ZPA
!     ZOUTPUT3(:,JK)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   ENDDO
!   IF(NACCUR == 1)THEN
!     ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
!   ENDIF
! !     total soil wetness
!   NVARID = NCVID(NPOS,'SoilWet',IERR)
!   ZVALUE(:)=ZSWET(:)
!   ZOUTPUT(:,1)=UNPACK(ZSWET,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF
ENDIF
      
IF(LWREVA)THEN
!* -- Evaporation components
!******************************************************************
  NPOS = NPOSEVA

!     interception evaporation
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'ECanop',IERR)
  ZVALUE(:)=D1SWLJQ2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     vegetation transpiration
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'TVeg',IERR)
  !ZVALUE(:)=(D1SWAEXT2(:,1,IA)+D1SWAEXT2(:,2,IA)+&
  ! &D1SWAEXT2(:,3,IA)+D1SWAEXT2(:,4,IA))*ZMM
  ZVALUE(:)=0._JPRB
  DO JK=1,NCSS
    ZVALUE(:)=ZVALUE(:)+D1SWAEXT2(:,JK,IA)*ZMM
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN  
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     excess condensation
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Conds',IERR)
  ZVALUE(:)=D1SW1C2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     bare soil evaporation
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'ESoil',IERR)
  ZVALUE(:)=D1SW1JBG2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
!     rootzone soil moisture
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'RootMoist',IERR)
  ZVALUE(:)=ZSROOT(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

! !     Canopy interception
!   NVARID = NCVID(NPOS,'CanopInt',IERR)
!   ZVALUE(:)=WRENUA(:)*ZPA
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF

!     Snow sublimation
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SubSnow',IERR)
  ZVALUE(:)=D1SWNJQ2(:,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     PotEvapI
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'PotEvapI',IERR)
  ZVALUE(:)=D1STIEVAPU2(:,4,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     PotEvapWA
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'PotEvapWA',IERR)
  ZVALUE(:)=D1STIEVAPU2(:,1,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     PotEvapU
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'PotEvapU',IERR)
  ZVALUE(:)=D1STIEVAPU2(:,2,IA)*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

  !     EWater
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'EWater',IERR)
  ZVALUE(:)=(D1STIEVAP2(:,1,IA)+D1STIEVAP2(:,9,IA))*ZMM
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     Aerodynamic Conductance (1/ra) on veg.
!  NVARID = NCVID(NPOS,'ACond',IERR)
!  ZVALUE(:)=ZACOND(:)
!  ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!  IF(NACCUR == 1)THEN
!    ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!  ELSE
!    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!  ENDIF
ENDIF
      
IF(LWRCLD)THEN
!* -- Cold-season processes
!******************************************************************
  NPOS = NPOSCLD

!     Snow fraction
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SnowFrac',IERR)
  ZVALUE(:)=ZSNOFR(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     Ice fraction
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'IceFrac',IERR)
  ZVALUE(:)=D1STIFR2(:,2,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Sea ice thickness
! JUST A MODEL CONSTANT
!      NVARID = NCVID(NPOS,'IceT',IERR)

!* -- Frozen soil depth
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Fdepth',IERR)
  ZVALUE(:)=ZFDEPT(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Depth to soil thaw
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Tdepth',IERR)
  ZVALUE(:)=ZFTHAW(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

! !* -- Snow albedo
!   NVARID = NCVID(NPOS,'SAlbedo',IERR)
! !      ZVALUE(:)=ASNNUA(:)*ZPA
!   WHERE(ZSNOFR(1:NPOI) == 0.)
!     ZVALUE(1:NPOI)=RMISS
!   ELSEWHERE
!     ZVALUE(1:NPOI)=D1SNAFR2(1:NPOI,IA)*ZWA/ZSNOFR(1:NPOI)
!   END WHERE
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF

!* -- Snow depth
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SnowDepth',IERR)
  ZVALUE(:)=ZSNDEP(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

! !* -- SNOW LIQUID WATER CONTENT
!   NVARID = NCVID(NPOS,'WSN',IERR)
!   ZVALUE(:)=D1WSN2(:,IA)*ZWA
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF
!     
!   !* -- heat changes due to rainfall freezing
!   NVARID = NCVID(NPOS,'HFLUXRF',IERR)
!   ZVALUE(:)=D1HFLUXRF2(:,IA)*ZWA
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF
!  
!   !* -- MASS advected by rainfall
!   NVARID = NCVID(NPOS,'MFLUXRF',IERR)
!   ZVALUE(:)=D1WFLUXRF2(:,IA)*ZMM
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF
 
ENDIF

IF(LWRLKE)THEN
!* -- LAKE SURFACE FLUXES
!******************************************************************
  NPOS = NPOSLKE
  JTILE=9
!    LAKE net shortwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LSWnet',IERR)
!   ZVALUE(:)=(D1SRFLD2(:,IA)+D1SRFLU2(:,IA))*ZWA
  ZVALUE(:)=(D1STISRD2(:,JTILE,IA)+D1STISRU2(:,JTILE,IA))*ZWA
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!    LAKE net longwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LLWnet',IERR)
!   ZVALUE(:)=(D1TRFLD2(:,IA)+D1TRFLU2(:,IA))*ZWA
  ZVALUE(:)=(D1STITRD2(:,JTILE,IA)+D1STITRU2(:,JTILE,IA))*ZWA
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN 
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!   LAKE  latent heat flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LQle',IERR)
!   ZVALUE(:)=D1AHFL2(:,IA)*ZWA
  ZVALUE(:)=D1STILE2(:,JTILE,IA)*ZWA
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!  LAKE   sensible heat flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LQh',IERR)
!   ZVALUE(:)=D1AHFS2(:,IA)*ZWA
  ZVALUE(:)=D1STIH2(:,JTILE,IA)*ZWA
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

! LAKE    upward longwave
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LLWup',IERR)
!   ZVALUE(:)=D1TRFLU2(:,IA)*ZWA
  ZVALUE(:)=D1STITRU2(:,JTILE,IA)*ZWA
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
  ! LAKE    evaporation
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LEvap',IERR)
!   ZVALUE(:)=D1STE2(:,IA)*ZMM
  ZVALUE(:)=D1STIEVAP2(:,JTILE,IA)*ZMM
  DO JL=1,NPOI
    ZVALUE(JL)=ZVALUE(JL)/(D1STIFR2(JL,JTILE,IA)*ZWA)
  ENDDO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN  
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
  !    LAKE surface radiative temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LSkinT',IERR)
!   ZVALUE(:)= D1SRADTK2(:,IA)*ZWA
  ZVALUE(:)=D1STITK2(:,JTILE,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN  
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
  !    LAKE fraction - fraction tile 9 
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Clake',IERR)
  ZVALUE(:)=D1STIFR2(:,JTILE,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
ENDIF

IF(LWROCD)THEN

!* -- Ocean mixed layer model
!******************************************************************
  NPOS = NPOSOCD

  IF(NDIMCDF == 2)THEN
    ISTART3(1)=IX1
    ISTART3(2)=IY1
    ISTART3(3)=ISLOT
    ICOUNT3(1)=IX2-IX1+1
    ICOUNT3(2)=IY2-IY1+1
    ICOUNT3(3)=1

    ISTART4(1)=IX1
    ISTART4(2)=IY1
    ISTART4(3)=1
    ISTART4(4)=ISLOT
    ICOUNT4(1)=IX2-IX1+1  !KPP
    ICOUNT4(2)=IY2-IY1+1  !KPP
    ICOUNT4(3)=NCOM+1     !KPP
    ICOUNT4(4)=1          !KPP
  ELSE
    ISTART3(1)=IST
    ISTART3(2)=ISLOT
    ICOUNT3(1)=NLEN
    ICOUNT3(2)=1

    ISTART4(1)=IST
    ISTART4(2)=1
    ISTART4(3)=ISLOT
    ICOUNT4O(1)=NLEN    !KPP
    ICOUNT4O(2)=NCOM+1  !KPP
    ICOUNT4O(3)=1       !KPP
  ENDIF

!     ocean depth
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'OceanD',IERR)
  ZVALUE(:)=VFOCDEPTH(:)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!     ocean vertical level
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'OceanL',IERR)
  DO JK=1,NCOM+1
    ZVALUE(:)=VFZO(:,JK)
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZBUF(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NCOM+1)=ZOUTPUT(:,1:NCOM+1)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4O,ZOUTPUTS(:,1:NCOM+1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4O,ZOUTPUT(:,1:NCOM+1),IERR)
    ENDIF
  ENDIF

ENDIF

!IF (LECTESSEL) THEN

IF(LWRCO2)THEN
!* -- CO2 fluxes
!******************************************************************
  NPOS = NPOSCO2
!* -- Ag
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Ag',IERR)
  ZVALUE(:)=D1SAG2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF  
  ENDIF

!* -- Rd
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Rd',IERR)
  ZVALUE(:)=D1SRD2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- An
  IF( MYPROC == 1 )NVARID = NCVID(NPOS,'An',IERR)
  ZVALUE(:)=D1SAN2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Rsoil_str = RESPIRATION FROM SOIL AND STRUCTURAL BIOMASS.
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Rsoil_str',IERR)
  ZVALUE(:)=D1SRSOIL_STR2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF 

!* -- Reco = ECOSYSTEM RESPIRATION.
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Reco',IERR)
  ZVALUE(:)=D1SRECO2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF 

!* -- CO2 flux = NEE
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'CO2flux',IERR)
  ZVALUE(:)=D1SCO2FLUX2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- CH4 flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'CH4flux',IERR)
  ZVALUE(:)=D1SCH4FLUX2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- BVOC flux
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'BVOCflux',IERR)
  ZVALUE(:)=D1SBVOCFLUX2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

ENDIF 

IF(LWRBIO)THEN
!* -- Biomass
!******************************************************************
  NPOS = NPOSBIO
!* -- LAI
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'lai',IERR)
  ZVALUE(:)=D1SLAI2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF  
  ENDIF
!* -- Active biomass
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'biomass',IERR)
  ZVALUE(:)=D1SBIOM2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Active biomass loss
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Bloss',IERR)
  ZVALUE(:)=D1SBLOSS2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
!* -- Active biomass gain
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Bgain',IERR)
  ZVALUE(:)=D1SBGAIN2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Above ground structural biomass
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Biomstr',IERR)
  ZVALUE(:)=D1SBIOMSTR2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF

!* -- Below ground structural biomass
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Biomstr2',IERR)
  ZVALUE(:)=D1SBIOMSTRB2(:,IA)*ZWA
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF
  ENDIF
  
ENDIF 

IF(LWRVEG)THEN
!* -- Vegetation variables
!******************************************************************
  NPOS = NPOSVEG

  IF(NDIMCDF == 2)THEN
    ISTART4(1)=IX1
    ISTART4(2)=IY1
    ISTART4(3)=1
    ISTART4(4)=ISLOT
    ICOUNT4(1)=IX2-IX1+1
    ICOUNT4(2)=IY2-IY1+1
    ICOUNT4(3)=NVHILO
    ICOUNT4(4)=1
  ELSE
    ISTART4(1)=IST
    ISTART4(2)=1
    ISTART4(3)=ISLOT
    ICOUNT4(1)=NLEN
    ICOUNT4(2)=NVHILO
    ICOUNT4(3)=1
  ENDIF

!* -- Canopy resistance
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'rc',IERR)
  DO JVT=1,NVHILO  
    ZVALUE(:)=D1SVTGC2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF  

!* -- Aerodynamic resistance
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'ra',IERR)
  DO JVT=1,NVHILO  
    ZVALUE(:)=D1SVTGA2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)= UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF

 !* -- Latent heat per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LEvt',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTLE2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF

 !* -- Stress function F2 per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'f2vt',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTF22(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF(NACCUR == 1)THEN
    ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
  ENDIF 

 !* -- Ds, specific humidity deficit at canopy level per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'dsvt',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTDS2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF

 !* -- Dmax, max specific humidity deficit per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'dmaxvt',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTDMAX2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF

ENDIF 

IF(LWREXT)THEN
!* -- Extra variables 30 MIN VALIDATION ALTERRA
!******************************************************************
  NPOS = NPOSEXT

! !* -- Snow fraction
!   NVARID = NCVID(NPOS,'snfr',IERR)
!   ZVALUE(:)=D1SNFR2(:,IA)*ZWA
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF

! !* -- Interception fraction
!   NVARID = NCVID(NPOS,'intfr',IERR)
!   ZVALUE(:)=D1SWFR2(:,IA)*ZWA
!   ZOUTPUT(:,1)=UNPACK(ZVALUE,LMASK(ISTP:IENP),RMISS)
!   IF(NACCUR == 1)THEN
!     ZOUTPUTS(:,1)=ZOUTPUT(:,1)
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
!   ELSE
!     CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
!   ENDIF  
  
!* -- Tair (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Tair',IERR)
  ZVALUE(:)=D1STAIR2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF  
  ENDIF

!* -- Qair (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qair',IERR)
  ZVALUE(:)=D1SQAIR2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF   

IF (LEAIRCO2COUP) THEN
!* -- CO2air (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'CO2air',IERR)
  ZVALUE(:)=D1SCO2AIR2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF   
ENDIF

 !* -- Uwind (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Uwind',IERR)
  ZVALUE(:)=D1SUWIND2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF
  
 !* -- Vwind (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Vwind',IERR)
  ZVALUE(:)=D1SVWIND2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF   
  ENDIF  
  
  !* -- Psurf (forcing T+1, end of time step)
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'psurf',IERR)
  ZVALUE(:)=D1SPSURF2(:,1)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1)=ZOUTPUT(:,1)
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS(:,1),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT(:,1),IERR)
    ENDIF 
  ENDIF    

ENDIF 

IF(LWRTIL)THEN
!* -- Tiled output
!******************************************************************
  NPOS = NPOSTIL

  IF(NDIMCDF == 2)THEN
    ISTART4(1)=IX1
    ISTART4(2)=IY1
    ISTART4(3)=1
    ISTART4(4)=ISLOT
    ICOUNT4(1)=IX2-IX1+1
    ICOUNT4(2)=IY2-IY1+1
    ICOUNT4(3)=NTILES
    ICOUNT4(4)=1
  ELSE
    ISTART4(1)=IST
    ISTART4(2)=1
    ISTART4(3)=ISLOT
    ICOUNT4(1)=NLEN
    ICOUNT4(2)=NTILES
    ICOUNT4(3)=1
  ENDIF

 !* -- Tile fraction
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'tifr',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=D1STIFR2(:,JTILE,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUT(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF
  ENDIF 

  !* -- Tiled Swnet
  IF( MYPROC ==1 ) NVARID = NCVID(NPOS,'SWnet',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STISRD2(:,JTILE,IA)+D1STISRU2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF

  !* -- Tiled Lwnet
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LWnet',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STITRD2(:,JTILE,IA)+D1STITRU2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF


 !* -- Tiled latent heat flux NOT normalized to grid square
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qle',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STILE2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF


 !* -- Tiled sensible heat flux NOT normalized to grid square
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Qh',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STIH2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF

  !* -- LWup
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'LWup',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=D1STITRU2(:,JTILE,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF

  !* --Evap
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Evap',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=D1STIEVAP2(:,JTILE,IA)*ZMM
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF

  !* -- Tiled Skin Temperature
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SkinT',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STITK2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF
  
  !* -- Tiled Skin thermal Conductivity
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SkinC',IERR)
  DO JTILE=1,NTILES
    ZVALUE(:)=(D1STISKC2(:,JTILE,IA))*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NTILES)=ZOUTPUT(:,1:NTILES)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NTILES),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NTILES),IERR)
    ENDIF 
  ENDIF

ENDIF

IF(LWRVTY)THEN
!* -- Output per vegetation type
!******************************************************************
  NPOS = NPOSVTY

  IF(NDIMCDF == 2)THEN
    ISTART4(1)=IX1
    ISTART4(2)=IY1
    ISTART4(3)=1
    ISTART4(4)=ISLOT
    ICOUNT4(1)=IX2-IX1+1
    ICOUNT4(2)=IY2-IY1+1
    ICOUNT4(3)=NVHILO
    ICOUNT4(4)=1
  ELSE
    ISTART4(1)=IST
    ISTART4(2)=1
    ISTART4(3)=ISLOT
    ICOUNT4(1)=NLEN
    ICOUNT4(2)=NVHILO
    ICOUNT4(3)=1
  ENDIF

!* -- Vegetation type fraction
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'vtfr',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTFR2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

!* -- LAI per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'lai',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTLAI2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

!* -- Active biomass per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'biomass',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTBIOM2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

!* -- Biomass loss per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Bloss',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTBLOSS2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF
  
 !* -- Biomass gain per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Bgain',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTBGAIN2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF

!* -- Below ground structural biomass per vegetation type
  IF( MYPROC ==1  )NVARID = NCVID(NPOS,'Biomstr',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTBIOMSTR2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF
  
!* -- Below ground structural biomass per vegetation type
  IF( MYPROC == 1 )NVARID = NCVID(NPOS,'Biomstr2',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTBIOMSTRB2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF  
  ENDIF
 
 !* -- Ag per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Ag',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTAG2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF  
  ENDIF
 
  !* -- Rd per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Rd',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTRD2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF 
  ENDIF 
  
  !* -- An per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'An',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTAN2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF  
  ENDIF
 
  !* -- Rsoil_str per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Rsoil_str',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTRSOIL_STR2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF  
  ENDIF
  
  !* -- Reco per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'Reco',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTRECO2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF  
  ENDIF 
  
  !* -- CO2 flux per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'CO2flux',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTCO2FLUX2(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF
 
  !* -- RnoQ10 per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'RnoQ10',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SVTRNOQ102(:,JVT,IA)*ZWA
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

  !* -- BVOC flux diagnostics type 1, specified per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'BVOCflux1',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SBVOCST1(:,JVT) ! *ZWA ! VH: Not accumulated ; no need to multiply..
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

  !* -- BVOC flux diagnostics type 2, specified per vegetation type
  IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'BVOCflux2',IERR)
  DO JVT=1,NVHILO
    ZVALUE(:)=D1SBVOCST2(:,JVT) ! *ZWA ! VH: Not accumulated ; no need to multiply..
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTDCDF:")
    IF( MYPROC == 1 ) ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDDO
  IF( MYPROC == 1 ) THEN
    IF(NACCUR == 1)THEN
      ZOUTPUTS(:,1:NVHILO)=ZOUTPUT(:,1:NVHILO)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTS(:,1:NVHILO),IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT(:,1:NVHILO),IERR)
    ENDIF
  ENDIF

ENDIF

IF( MYPROC == 1 ) THEN
  DO J=1,JPNCDF
    IF(LPOS(J) .AND. LNCSNC )THEN
      NPOS=IPOS(J)
      CALL NCSNC(NPOS,IERR)
    ENDIF
  ENDDO
ENDIF


DEALLOCATE (ZOUTPUT)    !INFLATED OUTPUT ARRAY
IF( NACCUR == 1 ) THEN
  DEALLOCATE (ZOUTPUTS)   !INFLATED OUTPUT ARRAY
ENDIF
DEALLOCATE (ZQLINUA)    !RESCALED SOIL WATER
DEALLOCATE (ZLIQ)       !LIQUID SOIL WATER
DEALLOCATE (ZSWET)      !TOTAL SOIL WETNESS
DEALLOCATE (ZSROOT)     !ROOT AVAILABLE SOIL WATER
DEALLOCATE (ZFDEPT)     !DEPTH OF FROZEN SOIL
DEALLOCATE (ZFTHAW)     !DEPTH OF THAW LAYER
DEALLOCATE (ZSNDEP)     !SNOW DEPTH
DEALLOCATE (ZSNOFR)     !SNOW FRACTION
DEALLOCATE (ZICEFR)     !ICE FRACTION
DEALLOCATE (ZACOND)
DEALLOCATE (ZVALUE)
DEALLOCATE (ZSSN)
DEALLOCATE (ZRSN)
DEALLOCATE (ZTSN)
DEALLOCATE (ZWSN)
DEALLOCATE (ZTMP1)

IF (LHOOK) CALL DR_HOOK('WRTDCDF',1,ZHOOK_HANDLE)

RETURN
END SUBROUTINE WRTDCDF
